package com.mgm.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mgm.dto.UserDto;
import com.mgm.exception.CustomException;
import com.mgm.pojo.User;
import com.mgm.result.IResult;
import com.mgm.user.service.UserService;

@Controller
@RequestMapping("/")
public class LoginController {
	
	private static Logger logger =LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserService userService;
	
	//用户登录
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@ResponseBody
	public IResult<UserDto> login(@RequestBody User user,
			HttpServletRequest request,	HttpServletResponse response) {
		logger.info("Login==============================================================");
//		Assert.notNull(user.getAccount(), "username can not be empty");
//      Assert.notNull(user.getPwd(), "password can not be empty");
		if(StringUtils.isEmpty(user.getAccount())) {
			throw new CustomException("帐号不能为空");
		}
		if(StringUtils.isEmpty(user.getPwd())) {
			throw new CustomException("密码不能为空");
		}
		IResult<UserDto> msg= userService.login(user.getAccount(), user.getPwd());
		return msg;
	}

}
